package a4_p04_jw_kw.graph;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
import java.util.Set;

import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.Pseudograph;

import a1_p04_jw_kw.graph.GraphCreater;
import a1_p04_jw_kw.struct.GraphSetup;
import a1_p04_jw_kw.struct.GraphTransition;
import a1_p04_jw_kw.struct.Node;


public class GraphEUBIGCreater {
	Pseudograph<Node, DefaultWeightedEdge> g; //the created graph


	GraphSetup gs;

	ArrayList<GraphTransition> transitions = new ArrayList<GraphTransition>();
	Node[] nodes;
	int targetNodeIndex;
	int startNodeIndex;
	int vertices = 200;
	int edgePerVertex = 50;

	ArrayList<Double> weights;

	public GraphEUBIGCreater() {
		nodes = new Node[vertices];
		weights = new ArrayList<Double>();

		//create all nodes
		for (int i = 1; i <= vertices; i++) {
			Node newNode = new Node(String.valueOf(i));
			nodes[i-1] = newNode;

		}


		for (int i = 1; i <= vertices * edgePerVertex; i++) {
			weights.add(trim(Double.valueOf(i * 0.1)));

		}

		Collections.shuffle(weights);

		//create edges for all nodes
		for (int i = 0; i < vertices; i++) {
			for (int t = 1; t <= edgePerVertex; t++) {
				int targetIndex = i + t;

				//reduce the target index if its "out of index"
				if (targetIndex >= vertices) {
					targetIndex = targetIndex - (vertices);

				}

				double weight = getRandomWeightFromList();

				if (weight == 0.0) {
					System.out.println("fail");

				}
				transitions.add(new GraphTransition(nodes[i], nodes[targetIndex], String.valueOf(weight)));

			}
		}

		createGraphSetup();
		GraphCreater gc = new GraphCreater(this.gs);
		gc.createGraph();
		this.g = (Pseudograph<Node, DefaultWeightedEdge>)gc.getGraph();

	}

	//create the GraphSetup
	private void createGraphSetup() {
		this.gs = new GraphSetup(false,true,true,true,transitions);

	}

	private int getRandomWeight() {
		Random generator = new Random();
		return generator.nextInt(100);

	}

	private double getRandomWeightFromList() {
		double ret = weights.get(0);
		weights.remove(0);

		return ret;
	}


	public Pseudograph<Node, DefaultWeightedEdge> getGraph() {
		return this.g;

	}

	public GraphSetup getGraphSetup() {
		return gs;
	}

	private double trim(double d) {
		return Math.floor(d * 1e1) / 1e1;
	}

	public int getVertexCount() {
		return vertices;
	}

	public int getEdgeCount() {
		return edgePerVertex;
	}



}
